package edu.njfu.mystudentapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
//import edu.njfu.homeork.service.StudentService
import edu.njfu.mystudentapp.model.ResultVO
import edu.njfu.mystudentapp.service.StudentService
import edu.njfu.mystudentapp.utils.ServerCreator
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var intentOK = Intent(this, StudentsInfo::class.java)

        loginBtn.setOnClickListener{
            val studentService= ServerCreator.create(StudentService::class.java)
            Log.d("dsfs",RegisterAccount.text.toString()+"   "+RegisterPassword.text.toString())
            studentService.getUserInfo(RegisterAccount.text.toString(),RegisterPassword.text.toString()).enqueue(object :retrofit2.Callback<ResultVO>{
                override fun onFailure(call: retrofit2.Call<ResultVO>, t: Throwable) {
                    Toast.makeText(this@MainActivity,"无法连接网络",Toast.LENGTH_SHORT).show()
                }
                override fun onResponse(
                        call: retrofit2.Call<ResultVO>,
                        response: retrofit2.Response<ResultVO>
                ) {
                    var resultVO=response.body()
                    if (resultVO != null) {
                        Log.d("dssdfsdffs",resultVO.result.toString())
                    }else{
                        Log.d("dssdfsdffs","saasfd")

                    }

                    if(resultVO?.result.toString()=="ok"){
                        startActivity(intentOK)
                    }else{
                        AlertDialog.Builder(this@MainActivity).apply {
                            setTitle("登录失败")
                            setMessage("账号密码错误")
                            setCancelable(false)
                            setPositiveButton("ok"){
                                dialog, which ->
                            }
                            show()
                        }

                    }
                }
            })

        }
        regBtn.setOnClickListener{
            var intent1= Intent(this,RegisterActivity::class.java)
            startActivity(intent1)
        }
    }

}